
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YBApproval.IDAL;
using YBApproval.Model;
using System.Data;

namespace YBApproval.EFDAL
{
    public class UserProfileDAL : IUserProfile
    {
        private YBDbContext _db = new YBDbContext();

        public List<UserProfile> GetAll()
        {
            return _db.UserProfiles.ToList();
        }

        public bool InsertNewUser(UserProfile nUser)
        {
            try
            {
                //if (string.IsNullOrEmpty(nUser.Id))
                //{
                //    throw new Exception("Null Id Ex");
                //}
                _db.UserProfiles.Add(nUser);
                _db.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                if (ex.Message == "Null Id Ex")
                {
                    throw ex;
                }
                return false;
            }
        }

        public bool UpdateUserProfile(UserProfile UserInfo)
        {
            try
            {
                UserProfile user = _db.UserProfiles.Where(u => u.Id == UserInfo.Id).SingleOrDefault();
                if (user !=null)
                {
                    user.Delegate = UserInfo.Delegate;
                    user.Email = UserInfo.Email;
                    user.LastUpdatedBy = UserInfo.Name;
                    user.LastUpdatedDt = DateTime.Now;
                    user.LoginName = UserInfo.LoginName;
                    user.Mobile = UserInfo.Mobile;
                    user.Name = UserInfo.Name;
                    user.Phone = UserInfo.Phone;
                    user.Pwd = UserInfo.Pwd;
                    user.RoleId = UserInfo.RoleId;

                    _db.Entry(user).State = EntityState.Modified;
                    _db.SaveChanges();
                    return true;
                } 
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteUser(List<UserProfile> Users)
        {
            try
            {
                foreach (var item in Users)
                {                    
                    _db.UserProfiles.Remove(item);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public UserProfile UserLogin(UserProfile UserInfo)
        {
            try
            {
                var use = _db.UserProfiles.SingleOrDefault(u => u.LoginName == UserInfo.LoginName && u.Pwd == UserInfo.Pwd);
                return use;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public UserProfile GetUserByID(int UserId)
        {
            try
            {
                var use = _db.UserProfiles.SingleOrDefault(u => u.Id == UserId);
                return use;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public int? GetUserIdByName(string UserName)
        {
            try
            {
                var use = _db.UserProfiles.SingleOrDefault(u => u.Name == UserName);
                if (use == null)
                    return null;
                return use.Id;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void Dispose()
        {
            _db.Dispose();
        }
    }
}

